﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.product
{
    public partial class list : Manage
    {
        public int channel_id;
        protected int pageSize = 12;
        protected int page;
        protected int pcount;
        protected int category_id;
        protected string property;
        protected string keywords;
        public Model.channel channel;
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            category_id = GetInt("category_id", 0);
            property = GetString("property");
            keywords = GetString("keywords");

            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                ShowParamError();
                return;
            }

            channel = new He.BLL.channel().GetModelByCache(channel_id);
            if (!IsPostBack)
            {
                chkLoginLevel("viewChannel" + channel.id);
                //绑定类别
                CategoryTreeBind(0, "所有类别", channel_id, this.ddlcategory_id);

                CookieHelper.Set("LastListUrl", Request.RawUrl);    //保存最后一次访问的列表页Url

                RptBind();

                this.btnTongbu.Visible = base.AllowTongbu(this.channel);
            }
        }

        protected void ddlcategory_id_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _category_id;
            if (int.TryParse(this.ddlcategory_id.SelectedValue.ToString(), out _category_id))
            {
                Response.Redirect("list.aspx?" + CombUrlTxt(this.channel_id, _category_id, this.keywords, this.property) + "page=0");
            }
            else
            {
                Response.Redirect("list.aspx?" + CombUrlTxt(this.channel_id, 0, this.keywords, this.property) + "page=0");
            }
        }

        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?" + CombUrlTxt(this.channel_id, this.category_id, this.keywords, this.ddlProperty.SelectedValue) + "page=0");
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?" + CombUrlTxt(this.channel_id, this.category_id, txtKeywords.Text.Trim(), this.property) + "page=0");
        }
        //设置操作
        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
            BLL.article bll = new BLL.article();
            Model.article model = bll.GetModel(id);
            switch (e.CommandName.ToLower())
            {
                case "ibtntop":
                    if (model.is_top == 1)
                        bll.UpdateField(id, "is_top=0");
                    else
                        bll.UpdateField(id, "is_top=1");
                    break;
                case "ibtnred":
                    if (model.is_red == 1)
                        bll.UpdateField(id, "is_red=0");
                    else
                        bll.UpdateField(id, "is_red=1");
                    break;
                case "ibtnhot":
                    if (model.is_hot == 1)
                        bll.UpdateField(id, "is_hot=0");
                    else
                        bll.UpdateField(id, "is_hot=1");
                    break;
            }
            SaveLogs(string.Format("[{0}管理]修改属性:{1}", channel.manage_title, model.title));
            Response.Redirect(Request.RawUrl);
        }

        #region 数据绑定
        private void RptBind()
        {
            He.BLL.article bll = new He.BLL.article();
            if (this.category_id > 0)
            {
                this.ddlcategory_id.SelectedValue = this.category_id.ToString();
            }
            this.txtKeywords.Text = this.keywords;
            this.ddlProperty.SelectedValue = this.property;
            string strWhere = CombSqlTxt(this.channel_id, this.category_id, this.keywords, this.property);

            this.rptList.DataSource = bll.GetPageList(this.pageSize, this.page, strWhere, channel.order_by);
            this.rptList.DataBind();

            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                if (cb.Checked)
                {
                    Model.article model = bll.GetModel(id);
                    //删除缩略图
                    FileHelper.DeleteFile(model.img_url);
                    //删除图片集合
                    if (!string.IsNullOrEmpty(model.img_urls))
                    {
                        string[] fileArr = model.img_urls.Split(',');
                        foreach (string fileTemp in fileArr)
                        {
                            FileHelper.DeleteFile(fileTemp);
                        }
                    }
                    //删除记录
                    bll.Delete(id);
                    //删除扩展属性值
                    new BLL.attr_value().DeleteByPid(id);
                }
            }
            SaveLogs(string.Format("[{0}管理]批量删除", channel.manage_title));
            ShowSuccess("批量删除成功啦！", Request.RawUrl);
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs(string.Format("[{0}管理]批量保存排序", channel.manage_title));
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }

        protected void btnRed_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    bll.UpdateField(id, "is_red=1");
                }
            }
            SaveLogs(string.Format("[{0}管理]批量设置推荐", channel.manage_title));
            ShowSuccess("批量设置成功啦！",Request.RawUrl);
        }

        protected void btnNotRed_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    bll.UpdateField(id, "is_red=0");
                }
            }
            SaveLogs(string.Format("[{0}管理]批量取消推荐", channel.manage_title));
            ShowSuccess("批量设置成功啦！", Request.RawUrl);
        }

        protected void btnHot_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    bll.UpdateField(id, "is_hot=1");
                }
            }
            SaveLogs(string.Format("[{0}管理]批量设置热门", channel.manage_title));
            ShowSuccess("批量设置成功啦！", Request.RawUrl);
        }

        protected void btnNotHot_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);

            He.BLL.article bll = new He.BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    bll.UpdateField(id, "is_hot=0");
                }
            }
            SaveLogs(string.Format("[{0}管理]批量取消热门", channel.manage_title));
            ShowSuccess("批量设置成功啦！", Request.RawUrl);
        }

        //G宝盆同步
        protected void btnTongbu_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);
            base.Tongbu(this.rptList, this.channel);
        }
    }
}
